package submarines.ui;

import java.util.LinkedList;
import java.util.Scanner;
import submarines.*;
import submarines.TurnManager.turnAction;

/**
 *
 * @author elad
 */
   public class ConsoleUi implements Ui {
   public static final int MAX_WIDTH = 80;
   public static final int MAX_HEIGHT = 24;
   
   Scanner scanner = new Scanner(System.in);

   private enum tableChars {
       RIGNT_ENDING,
       MIDDLE,
       VERTICAL_SEPERATOR,
       HORIZONTAL_SEPERATOR,
       BOTTOM_ENDING,
       BOTTOM_RIGHT_CORNER;
       

        @Override
       public String toString() {
           char ch;
           String s;
           
           switch (this) {
               case RIGNT_ENDING:
                   ch = '+';
                   break;
               case MIDDLE:
                   ch = '+';
                   break;
               case VERTICAL_SEPERATOR:
                   ch = '|';
                   break;
               case HORIZONTAL_SEPERATOR:
                   ch = '-';
                   break;
               case BOTTOM_ENDING:
                   ch = '+';
                   break;
               case BOTTOM_RIGHT_CORNER:
                   ch = '+';
                   break;
               default:
                   ch = ' ';
                   break;
           }
           
           s = "" + ch;
           return s;
       };
   }

   /*@Override
   public int GetRow() {
       System.out.println("Please enter the row:(1-" + (Config.BOARD_SIZE) + ") ");
       int row = scanner.nextInt();
       scanner.nextLine();
       row--;
       return row;
   }
   
   @Override
   public int GetCol() {
       
       boolean valid = false;
       System.out.println("Please enter the column: (A-" + (char)('A' + Config.BOARD_SIZE - 1) + ")");
       String colString = scanner.nextLine();
       char colChar = colString.charAt(0);
       int colInt = colChar - 'A';
       if ((colChar >= 'a') && (colChar <= 'z')) {
           colInt = colChar - 'a';
       } else {
           colInt = colChar - 'A';
       }
       return colInt;
    }
    * 
    */

    @Override
    public String getTargetedPlayer(LinkedList<String> list) {
        System.out.println("Please enter the enemy player number you want to atteck from the following list: ");
        int listSize = list.size();
        int i;
        for(i = 0; i<listSize; i++ )
        {
            String name = list.get(i);
            System.out.println((i+1) + ". " + name + "");
        }
         int answer = scanner.nextInt();
         while (answer > listSize || answer < 1)
         {
             System.out.println("No such player please enter player number: ");
             answer = scanner.nextInt();
         }
         String currName = list.get(answer - 1);
         scanner.nextLine();
         return currName;
    }
    
    @Override
    public void printComputerMove(String attacker, enemyBoard board, int row, int col) {
        System.out.println(attacker + " Has attacked " + board.getBelongsTo() + " at coordinates " + (row + 1) + "" + (char)('A' + col));
        printBoard(board);
        scanner.nextLine();
    }

    @Override
    public void printEnemyBoard(enemyBoard board) {
     printBoard(board);
        int remaining[] = board.getRemainingSubs();
        int i;
        System.out.println("Remaining ships:");
        for (i = 0; i < remaining.length; i++) {
            int size = i+1;
            if (remaining[i] > 0)
                System.out.print("" + remaining[i] + " of size " + size + "   ");
        }
        System.out.println();
    }

    @Override
    public String askUserYesNoQuestions(String question) {
         System.out.println(question);
         String answer = scanner.nextLine();
         return answer;
    }
   
 
//TODO: ConsoleUI needs to be implemented
    @Override
    public String getMissleCoords() {
        System.out.println("Enter point to hit: ");
        String point = scanner.next();
        return point;
    }

    @Override
    public int getNumberOfPlayers() {
        System.out.println("Please enter players number from "+Config.MIN_PLAYERS+" to "+Config.MAX_PLAYERS+":");
        int playerNum = scanner.nextInt();
        return playerNum;
    }

    @Override
    public String getPlayerName() {
        System.out.println("Please enter player name  ");
        String name = scanner.next();
        return name;
    }

    @Override
    public playerType getPlayerType() {
        System.out.println("What is the player type by order? \n 1. Human \n 2. Computer" );
        int playerType = scanner.nextInt();
        while(playerType<1 || playerType>2)
        {
            System.out.println("Invalid input. \n What is the player type by order? \n 1. Human \n 2. Computer");
            playerType = scanner.nextInt();
        }
        if (playerType == 1)
            return Ui.playerType.HUMAN_PLAYER;
        else
            return Ui.playerType.COMPUTER_PLAYER;
    }

    @Override
    public TurnManager.turnAction getPlayersNextMove() {
        System.out.println("What do you want to do?");
        int i = 1;
        TurnManager.turnAction  ret;
        for (turnAction t : TurnManager.turnAction.values()) {
            System.out.println(i + ": " + t.toString());
            i++;
        }
        int action = scanner.nextInt();
        if (action > i)
            return null;
        i = 1;
        for (turnAction t : TurnManager.turnAction.values()) {
            if (action == i)
                return t;
            i++;
        }
        return null;
    }

    @Override
    public boolean addAnotherPlayer() {
        System.out.println("Would you like to add another player? yes/no ");
        String answer = scanner.next();
        if ("yes".equals(answer))
            return true;
        else
            return false;
    }

    @Override
    public void printAlert(String msg) {
        String s;
        int i, mid;
        mid = MAX_HEIGHT/2 - 1;
        for (i = 0; i < mid; i++)
            System.out.println();
        System.out.println("            " + msg);
        System.out.println("            Press ENTER to continue");
        for (i = mid+1; i < MAX_HEIGHT; i++)
            System.out.println();
        s = scanner.nextLine();
    }

    @Override
    public void printBoard(Board toPrint) {
        Square s;
        char ch = 'A';
        int i,j;
        
        String player = toPrint.getPlayerOwner().getName();
        //ClearScreen();
        System.out.print(player + " Board: \n");
        System.out.print("  ");
        for (i = Config.BOARD_START; i < Config.BOARD_SIZE + Config.BOARD_START; i++) {
            System.out.print(tableChars.VERTICAL_SEPERATOR + "" + ch);
            ch ++;
        }
        System.out.println(tableChars.VERTICAL_SEPERATOR);
        
        for (i = 1 + Config.BOARD_START; i <= Config.BOARD_SIZE + Config.BOARD_START; i++) {
            System.out.print(tableChars.HORIZONTAL_SEPERATOR + "" + tableChars.HORIZONTAL_SEPERATOR);
            for (j = 0; j < Config.BOARD_SIZE; j++) {
                System.out.print(tableChars.MIDDLE + "" + tableChars.HORIZONTAL_SEPERATOR);
            }
            System.out.println(tableChars.RIGNT_ENDING);
            
            System.out.printf("%2d", i);
            for (j = Config.BOARD_START; j < Config.BOARD_SIZE + Config.BOARD_START; j++) {
                s = toPrint.getSquare(i - 1, j);
                System.out.print(tableChars.VERTICAL_SEPERATOR + "" + s.getSign());
            }
            System.out.println(tableChars.VERTICAL_SEPERATOR);
        }
           System.out.print(tableChars.HORIZONTAL_SEPERATOR + "" + tableChars.HORIZONTAL_SEPERATOR);
           for (j = Config.BOARD_START; j < Config.BOARD_SIZE + Config.BOARD_START; j++) {
                System.out.print(tableChars.BOTTOM_ENDING+ "" + tableChars.HORIZONTAL_SEPERATOR);
            }
            System.out.println(tableChars.BOTTOM_RIGHT_CORNER);
         
    }

    @Override
    public void clearScreen() {
        int i;
       for (i = 0; i<= MAX_HEIGHT; i++) {
            System.out.println();
        }
    }
    
    @Override
    public int getSubmarineDirection(int subSize) {
        System.out.println("Please enter details for "+subSize+" square submarine, What is the submarine direction? \n 1. Horizontal \n 2. Vertical");
        int direction = scanner.nextInt();
        while (direction<1 || direction > 2)
        {
            direction = scanner.nextInt();
        }
        scanner.nextLine();
        return direction;
    }
    
     @Override
    public int getSubmarineRow(int subSize, int minRow, int maxRow) {
        int minRowToPrint = minRow+1;
        int maxRowToPrint = maxRow+1;
        System.out.println("What is the submarine row, choose between  " + minRowToPrint + " to  "+maxRowToPrint+"?");
        String s = scanner.nextLine();
        char r = s.charAt(0);
        int row = r - '0';
        while (row > maxRow || row < minRow)
        {
            System.out.println("Invalid input. \nWhat is the submarine row, choose between  " + minRowToPrint + " to  "+maxRowToPrint+"?");
            s = scanner.nextLine();
            r = s.charAt(0);
            row = r - '0';
        }
        row--;
        return row;
    }
     
     @Override
    public int getSubmarineCol(int subSize, int minCol, int maxCol) {
         String  colString;
         //colString = scanner.nextLine();
         char colChar;
         char minUpperChar = (char) ('A' + minCol + Config.BOARD_START);
         char maxUpperChar = (char) ('A' + maxCol + Config.BOARD_START);
         char minLowerChar = (char) (minUpperChar + 'a' - 'A');
         char maxLowerChar = (char) (maxUpperChar + 'a' - 'A');
         System.out.println("What is the submarine column, choose between  " + minUpperChar + " to  "+ maxUpperChar +"?");
         colString = scanner.nextLine();

        while (colString.length() > 1 || colString.length() < 1 || (!colString.matches("^[" + minLowerChar + "-" + maxLowerChar + "" + minUpperChar + "-" + maxUpperChar + "]+$")))
        {
            System.out.println("Invalid input. \nWhat is the submarine column, choose between  " + minUpperChar + " to  "+ maxUpperChar +"?");
            colString = scanner.nextLine();
        }
        colChar = colString.charAt(0);
        int colInt = 0;
        if ((colChar >= 'a') && (colChar <= 'z')) {
            colInt = colChar - 'a';
        } else {
            colInt = colChar - 'A';
        }
        return colInt;
    }
     
     @Override
    public Main.quitAction getNewGameAswer() {
        System.out.println("What do you want to do?");
        int i = 1;
        Main.quitAction  ret;
        for (Main.quitAction t : Main.quitAction.values()) {
            System.out.println(i + ": " + t.toString());
            i++;
        }
        int action = scanner.nextInt();
        if (action > i)
            return null;
        i = 1;
        for (Main.quitAction t : Main.quitAction.values()) {
            if (action == i)
                return t;
            i++;
        }
        return null;
    }
}
